import type { UserInfo } from '@vben/types';

import { requestClient } from '#/api/request';

/**
 * 系统信息接口
 */
export interface SystemInfo {
  id: number;
  systemName: string;
  url: string;
  desc: string;
  image: string;
  loginCallbackUrl: string;
}

/**
 * 获取用户信息
 */
export async function getUserInfoApi(): Promise<UserInfo> {
  const response = await requestClient.get<any>('/user/detail');

  // 将后端字段映射为前端期望的字段
  return {
    userId: response.id?.toString() || '',
    username: response.userName || '',
    realName: response.realName || '',
    avatar: response.userIcon || '', // 将 userIcon 映射为 avatar
    roles: [], // 角色信息可能需要从其他接口获取
    desc: response.email || '',
    token: '', // token 由认证流程处理
  };
}

/**
 * 获取用户拥有的系统列表
 */
export async function getUserSystemListApi(): Promise<SystemInfo[]> {
  return await requestClient.get<SystemInfo[]>('/user/system/list');
}

/**
 * 调用系统登录回调接口
 */
export async function callSystemLoginApi(
  callbackUrl: string,
  systemId: number,
): Promise<void> {
  return await requestClient.post<void>(callbackUrl, { systemId });
}
